package com.windowes.weichang.service;

import com.windowes.weichang.common.ServerResponse;
import com.windowes.weichang.dao.DemandMapper;
import com.windowes.weichang.dao.DemandProjectMapper;
import com.windowes.weichang.dao.WUserMapper;
import com.windowes.weichang.dto.OrderTypeInfo;
import com.windowes.weichang.dto.StaticInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/***
 *
 * @author: yangqing
 * @email: qing.yang@bangdao-tech.com
 * @createTime: 2018/6/18 下午5:05
 * @description:
 *
 **/
@Service
public class StatisticService {


    @Autowired
    DemandMapper demandMapper;
    @Autowired
    DemandProjectMapper projectMapper;
    @Autowired
    WUserMapper userMapper;

    /**
     * 首页统计信息
     *
     * @return
     */
    public ServerResponse getHome() {
        StaticInfo staticInfo = new StaticInfo();
        staticInfo.setBenefit(new BigDecimal(0));
        staticInfo.setCustomerCount(userMapper.countByType(1));
        staticInfo.setOrderCount(demandMapper.getCount());
        staticInfo.setUnHandleOrderCount(demandMapper.getUnHandleOrderCount());
        staticInfo.setUnRebackOrderCount(demandMapper.getUnRebackOrderCount());
        List<OrderTypeInfo> orderTypeInfos = new ArrayList<>();
        OrderTypeInfo orderTypeInfo = new OrderTypeInfo();
        Integer fixCount = projectMapper.countByType(0);
        orderTypeInfo.setCount(fixCount);
        BigDecimal sumPrice = projectMapper.sumPrice(0);
        if (fixCount == 0) {
            orderTypeInfo.setAvgPrice(new BigDecimal(0d));
        } else {
            BigDecimal divide = sumPrice.divide(new BigDecimal(fixCount), RoundingMode.FLOOR);
            orderTypeInfo.setAvgPrice(divide);
        }
        orderTypeInfo.setName("修门窗");
        orderTypeInfos.add(orderTypeInfo);

        orderTypeInfo = new OrderTypeInfo();
        Integer changeCount = projectMapper.countByType(1);
        orderTypeInfo.setCount(changeCount);
        orderTypeInfo.setName("换门窗");
        BigDecimal sumChangePrice = projectMapper.sumPrice(1);
        if (changeCount == 0) {
            orderTypeInfo.setAvgPrice(new BigDecimal(0d));
        } else {
            BigDecimal divide = sumChangePrice.divide(new BigDecimal(changeCount), RoundingMode.FLOOR);
            orderTypeInfo.setAvgPrice(divide);
        }
        orderTypeInfos.add(orderTypeInfo);
        staticInfo.setOrderTypeInfos(orderTypeInfos);
        return ServerResponse.createBySuccess("获取成功!", staticInfo);
    }
}
